package ebusiness.ebusiness.Controllers;

import ebusiness.ebusiness.Dao.OrderDao.OrderQueryVO;
import ebusiness.ebusiness.Dao.TorlleyDao.TorlleyQueryVO;
import ebusiness.ebusiness.Dao.UserDao.UserQueryVO;
import ebusiness.ebusiness.Dao.WalletDao.WalletQueryVO;
import ebusiness.ebusiness.Entity.*;
import ebusiness.ebusiness.Mapper.JsonUtil.StatusUtil;
import ebusiness.ebusiness.Mapper.PathUtil.WebPath;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.CartView;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.TorlleyImp;
import ebusiness.ebusiness.Models.Front.ShoppingTorlley.Toy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class PayController2 {





    @RequestMapping(path = WebPath.AddADRESS,method = RequestMethod.POST)
    public String AddAdress( HttpSession session,String name,String address_province,String address_city,String address,String address_postal)throws Exception {
        int id = (int) session.getAttribute("user");

        if (id < 0) {
            //未登录
            throw new Exception("not logged in");
        } else {

            UserQueryVO userQueryVO = new UserQueryVO();
            try {
                Address myaddress = new Address();

                myaddress.setAddress_province(address_province);
                myaddress.setAddress_city(address_city);
                myaddress.setAddress(address);
                myaddress.setAddress_postal(address_postal);
                myaddress.setCustomer_name(name);
                myaddress.setCustomer_id(id);
                myaddress.setAddress_id(userQueryVO.GeneralAddressID());

                userQueryVO.addAddress(myaddress);
            } catch (Exception e) {
                throw e;
            }
            return "1";
        }
    }
    @RequestMapping(path = WebPath.ModifyAddress,method = RequestMethod.POST)
    public String ModifyAddress(HttpSession session,String name,String address_province,String address_city,String address,String address_postal,String address_id)throws Exception{
        int id = (int)session.getAttribute("user");
        if( id < 0 ){
            //未登录
            throw new Exception("not logged in");
        }else {
            int addr_id = Integer.valueOf(address_id);
                UserQueryVO userQueryVO = new UserQueryVO();
            try {
                Address myaddress = new Address();
                myaddress.setAddress_id(addr_id);
                myaddress.setAddress_province(address_province);
                myaddress.setAddress_city(address_city);
                myaddress.setAddress(address);
                myaddress.setAddress_postal(address_postal);
                myaddress.setCustomer_name(name);
                myaddress.setCustomer_id(id);
                userQueryVO.modifyAddress(myaddress);
            }catch (Exception e){
                throw e;
            }
            return "1";

        }

    }

    @RequestMapping(path = WebPath.DelADDRESS,method = RequestMethod.POST)
    public String DelAddress(HttpSession session,String address_id)throws Exception{
        int id = (int)session.getAttribute("user");
        if( id < 0 ){
            //未登录
            throw new Exception("not logged in");
        }else {
            int addr_id = Integer.valueOf(address_id);
            UserQueryVO userQueryVO=new UserQueryVO();
            //userQueryVO. 删除地址
            try {
                userQueryVO.removeAddress(addr_id);
            }catch (Exception e){
                return "0";
            }
            return "1";
        }
    }

}
